package com.gxt.order.service.impl;

import com.gxt.aftermarket.domain.Guarantee;
import com.gxt.aftermarket.mapper.GuaranteeMapper;
import com.gxt.finance.domain.Deposit;
import com.gxt.finance.domain.Receivables;
import com.gxt.finance.domain.ReceivablesItem;
import com.gxt.finance.mapper.DepositMapper;
import com.gxt.finance.mapper.ReceivablesItemMapper;
import com.gxt.finance.mapper.ReceivablesMapper;
import com.gxt.order.domain.Contract;
import com.gxt.order.domain.Order;
import com.gxt.order.dto.OrderPayDTO;
import com.gxt.order.mapper.ContractMapper;
import com.gxt.order.mapper.OrderMapper;
import com.gxt.order.service.IOrderService;
import com.gxt.basic.service.impl.BaseServiceImpl;
import com.gxt.sysmanage.domain.Dictionaryitem;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gxt
 * @since 2022-10-26
 */
@Transactional
@Service
public class OrderServiceImpl extends BaseServiceImpl<Order> implements IOrderService {
    @Autowired
    private GuaranteeMapper guaranteeMapper;//保修单
    @Autowired
    private ContractMapper  contractMapper;//合同
    @Autowired
    private ReceivablesMapper receivablesMapper;//应收款
    @Autowired
    private ReceivablesItemMapper receivablesItemMapper;//应收款明细
    @Autowired
    private OrderMapper orderMapper;//订单
    @Autowired
    private DepositMapper depositMapper;//定金
    @Override
    public void pay(OrderPayDTO orderPayDTO) {
        Long orderId = orderPayDTO.getOrderId();
        Dictionaryitem payMethod = orderPayDTO.getPayMethod();
        Order order = orderMapper.loadById(orderId);
        Deposit deposit = depositMapper.loadByOrderSn(order.getSn());
        //创建合同
        Contract contract = new Contract();
        contract.setSn(UUID.randomUUID().toString());
        contract.setOrderId(orderId);
        contract.setOrderSn(order.getSn());
        contract.setCustomerId(order.getCustomerId());
        contract.setCustomerName(order.getCustomerName());
        contract.setPrice(order.getPrice());
        /**
         * 定金
         */
        if (Objects.nonNull(deposit)) {
            //当定金不为空时给合同设置定金
            contract.setDeposit(deposit.getDeposit());
        }
        contract.setContent("收取" + order.getCustomerName() + "购买" + order.getProductName() + "所用" + order.getPrice() + "元");
        contract.setCreateTime(new Date());
        contract.setState(1);
        contractMapper.insert(contract);

        //创建保修单
        Guarantee guarantee = new Guarantee();
        guarantee.setProductId(order.getProductId());
        guarantee.setProductName(order.getProductName());
        guarantee.setOrderId(orderId);
        guarantee.setOrderSn(order.getSn());
        guarantee.setCustomerId(order.getCustomerId());
        guarantee.setCustomerName(order.getCustomerName());
        guarantee.setStartTime(new Date());
        guarantee.setEndTime(DateUtils.addMonths(new Date(), 12));
        guarantee.setState(1);
        guaranteeMapper.insert(guarantee);

        //修改订单状态
        order.setState(2);
        orderMapper.update(order);

        //创建应收款
        Receivables receivables = new Receivables();
        receivables.setContractId(order.getId());
        receivables.setCustomerId(order.getCustomerId());
        receivables.setCustomerName(order.getCustomerName());
        receivables.setProductId(order.getProductId());
        receivables.setProductName(order.getProductName());
        receivables.getPrice(order.getPrice());
        receivables.setPaymentMethodId(payMethod.getId());
        receivables.setCreateTime(new Date());
        receivablesMapper.insert(receivables);

        handleReceivablesItem(order,deposit,contract,payMethod,receivables.getId());
    }



        private void handleReceivablesItem(Order order,Deposit deposit,Contract contract,Dictionaryitem payMethod,Long receivablesId){
            BigDecimal price = order.getPrice();
            if (Objects.nonNull(deposit)){
                //定金有值不为空 金额 = 订单金额 - 定金金额
                price = price.subtract(deposit.getDeposit());
                //创建应收款明细  定金也需要存到明细
                saveReceivablesItem(order,deposit.getDeposit(),contract,receivablesId,deposit.getPayTime());

            }
            if (payMethod.getValue().equals(0)){
                //全款付也只插入一条数据
                saveReceivablesItem(order,price,contract,receivablesId,new Date());
            }else{
                //分期  期数*12 条数据
            Integer payNum = Integer.parseInt(payMethod.getValue())*12;
            for (int i=1;i<=payNum;i++){
                BigDecimal divide = price.divide(new BigDecimal(payNum),BigDecimal.ROUND_UP);
                saveReceivablesItem(order,divide,contract,receivablesId,null);
            }
            }
        }
        private void saveReceivablesItem(Order order, BigDecimal price, Contract contract, Long receivablesId, Date payTime){
            ReceivablesItem receivablesItem = new ReceivablesItem();
            receivablesItem.setName("收到客户"+order.getCustomerName()+"缴纳的定金"+price);
            receivablesItem.setContractId(contract.getId());
            receivablesItem.setCustomerId(order.getCustomerId());
            receivablesItem.setCustomerName(order.getCustomerName());
            receivablesItem.setProductId(order.getProductId());
            receivablesItem.setProductName(order.getProductName());
            receivablesItem.setPrice(price);
            receivablesItem.setPaymethodId(1L);
            receivablesItem.setPayTime(payTime);
            receivablesItem.setReceivablesId(receivablesId);
            receivablesItem.setState(2);
            receivablesItem.setPayee(1L);
            receivablesItem.setPayeeName("admin");
            receivablesItemMapper.insert(receivablesItem);
        }
    }

